#!/bin/bash

set +e

if [ -z "$1"  ]
then
	echo "Example: start_node_worker.sh xx xx"
	exit 1;
fi

if [ -z "$2"   ]
then
	echo "截止到第几个？。。"
	exit 1;
fi

task="p2six"
if [ -z "$3"  ]; then
	echo "Example: p2six  2c2  4c2"
	exit 1;
else
  task=$3
fi

# task="p2six"
# if [ $3 == '2c2' ]; then
#   task="p2toc2"
# elif [ $3 == '4c2'   ]; then
#   task="c2"
# fi


if [ -z "$4"   ]
then
  echo "请输入序号: 1, 2, 3, 4, 5, 6"
  exit 1;
fi

if(($4!=1 && $4!=2 && $4!=3 && $4!=4 && $4!=5 && $4!=6)); then
  echo "启动序号错误..."
  exit 1;
fi

group=$5
if [ -z "$5"   ]
then
#   echo "请设置组别"
#   exit 1;
  echo "默认为: 0"
  group="0"
  
fi

IPPrefix="192.168.8."
curPath=$(dirname $(readlink -f "$0"))
PASSWD=$(cat $curPath/../.env)

# LOTUS_TOKEN='/usr/local/bin/lotus auth api-info --perm admin'
MINER_TOKEN=$(/usr/local/bin/lotus-miner auth api-info --perm admin)
method="SectorsListInStates"

# MINER_TOKEN=${MINER_TOKEN//0.0.0.0/10.0.1.200}

echo "start worker to $IPPrefix$1 - $2  ===> $3"

# /bin/bash ./start_gpu_worker_p2.sh 246 1

for((i=$1;i<$2;i++));
do
	echo "------link--> $IPPrefix$i---------"

	if(($i==2 || $i==34 || $i==200)); then
		echo "跳过..."
		continue
	fi

	if(($i==133)); then
    echo "不存在..."
    echo "--------------------------------"
    echo ""
    continue
  fi

	if(($i==137 || $i==138 || $i==139 || $i==140 || $i==141)); then
		echo "暂不启动..."
    echo "--------------------------------"
    echo ""
		continue
	fi

	# echo "开始进程....."
	sshpass -p $PASSWD ssh -o ConnectTimeout=4 ceshi@$IPPrefix$i "rm start_$task.sh"

	scp /home/ceshi/miner-sh/worker/start_$task.sh $IPPrefix$i:~

	sshpass -p $PASSWD ssh -o ConnectTimeout=4 ceshi@$IPPrefix$i "/bin/bash start_$task.sh 'LOTUS_TOKEN' $MINER_TOKEN $4 $i $group"

	
	sshpass -p $PASSWD ssh -o ConnectTimeout=4 ceshi@$IPPrefix$i 'sh -c "lotus-worker --version && ps -ef | grep lotus"'

	echo "--------------------------------"
  echo ""
	sleep 1
done

#  bash ./start_gpu_worker.sh 246 247 p2six 1 14
#  bash ./start_gpu_worker.sh 246 247 p2six 1 14
#  bash ./start_gpu_worker.sh 228 234 4c2 1 0
#  bash ./start_gpu_worker.sh 246 247 2c2 1 0

# Ubuntu中查看大于1G的文件：
# sudo find / -type f -size +1024000k -exec du -h {} \;

